package com.lo.controller;

import com.github.pagehelper.PageInfo;
import com.lo.common.*;
import com.lo.entity.TextualMaterial;
import com.lo.service.TextualMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;

@RestController
@RequestMapping("textualMaterial")
public class TextualMaterialController {

    @Autowired
    private TextualMaterialInfo textualMaterialInfo;
    @Autowired
    private TextualMaterialService textualMaterialService;
    @Autowired
    private ImageInfo imageInfo;

    @RequestMapping("addNewTextualMaterial")
    public JsonResult addNewTextualMaterial(TextualMaterial textualMaterial, MultipartFile textualMaterialFile,MultipartFile videoCover){
        JsonResult textualMaterialResult = FileUploadUtils.upload(textualMaterialFile, textualMaterialInfo.getBasePath(),"textualmaterial" );
        JsonResult coverResult =  FileUploadUtils.upload(videoCover,imageInfo.getBasePath(),"cover");
        textualMaterial.setUrl((String) textualMaterialResult.getData());
        textualMaterial.setCoverUrl((String) coverResult.getData());
        return textualMaterialService.addNewTextualMaterial(textualMaterial);
    }

    @RequestMapping("deleteTextualMaterial")
    public JsonResult deleteTextualMaterial(@PathParam("id") Long id){
        return textualMaterialService.deleteTextualMaterial(id);
    }

    @RequestMapping("getWordCloud")
    public JsonResult getWordCloud(String url){
//        System.out.println(url);
        String path = textualMaterialInfo.getBasePath()+"/"+url;
        return textualMaterialService.getWordCloud(path);
    }

    @RequestMapping("getTextSummary")
    public JsonResult getTextSummary(String url){
        String path = textualMaterialInfo.getBasePath()+"/"+url;
        return textualMaterialService.getTextSummary(path);
    }

    @RequestMapping("updateTextIsDelete")
    public JsonResult updateTextIsDelete(Long id){
        return textualMaterialService.updateTextIsDelete(id);
    }

    @RequestMapping("selectTextById")
    public JsonResult selectTextById(Long id){
        return textualMaterialService.selectTextById(id);
    }

    @RequestMapping("updateText")
    public JsonResult updateText(TextualMaterial textualMaterial){
        return textualMaterialService.updateText(textualMaterial);
    }
    @RequestMapping("selectAllTexts")
    public JsonResult selectAllTexts(){
        return textualMaterialService.selectAllTexts();
    }

    @RequestMapping("selectOrdinaryTexts")
    public JsonResult selectOrdinaryTexts(){
        return textualMaterialService.selectOrdinaryTexts();
    }

    @RequestMapping("selectVipTexts")
    public JsonResult selectVipTexts(){
        return textualMaterialService.selectVipTexts();
    }

    @RequestMapping("selectAllTextsWithPage")
    public JsonResult selectAllTextsWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                              String search){
        PageInfo pageInfo = textualMaterialService.selectAllTextsWithPage(pageNum,pageSize,search);
//        System.out.println(search);
        return new JsonResult<>(0,"",pageInfo);
    }

    @RequestMapping("selectOrdinaryTextsWithPage")
    public JsonResult selectOrdinaryTextsWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                  @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                  String search){
        return new JsonResult(0,"",textualMaterialService.selectOrdinaryTextsWithPage(pageNum,pageSize,search));
    }

    @RequestMapping("selectVipTextsWithPage")
    public JsonResult selectVipTextsWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                             String search){
        return new JsonResult(0,"",textualMaterialService.selectVipTextsWithPage(pageNum,pageSize,search));
    }

    @RequestMapping("selectEngineeringTextWithPage")
    public JsonResult selectEngineeringTextWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                             String search){
        return new JsonResult(0,"",textualMaterialService.selectEngineeringTextWithPage(pageNum,pageSize,search));
    }
    @RequestMapping("selectScienceTextWithPage")
    public JsonResult selectScienceTextWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                    String search){
        return new JsonResult(0,"",textualMaterialService.selectScienceTextWithPage(pageNum,pageSize,search));
    }
    @RequestMapping("selectLiteratureTextWithPage")
    public JsonResult selectLiteratureTextWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                String search){
        return new JsonResult(0,"",textualMaterialService.selectLiteratureTextWithPage(pageNum,pageSize,search));
    }
    @RequestMapping("selectLawTextWithPage")
    public JsonResult selectLawTextWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                   String search){
        return new JsonResult(0,"",textualMaterialService.selectLawTextWithPage(pageNum,pageSize,search));
    }

    @RequestMapping("selectAllTextual")
    public JsonResult selectAllTextual(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                      @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                      @RequestParam(value = "controllInfo")String controllInfo,
                                      String search){

//        System.out.println("textual is requesting："+pageNum+" "+pageSize+" "+controllInfo+" "+search);
        return textualMaterialService.selectAllTextual(pageNum,pageSize,search,controllInfo);
    }
    //删除课程
    @RequestMapping("deleteTextual/{id}")
    public JsonResult deleteCourse(@PathVariable Long id){
//        System.out.println("it is in coming");
        JsonResult result = textualMaterialService.deleteTextual(id);
        TextualMaterial textualMaterial = (TextualMaterial) result.getData();
        FileDeleteUtils.delete(textualMaterialInfo.getBasePath(),textualMaterial.getUrl());
        FileDeleteUtils.delete(imageInfo.getBasePath(),textualMaterial.getCoverUrl());
//        System.out.println("it is ok");
        return result;
    }
    @RequestMapping("updateTextExamine/{id}")
    public JsonResult updateTextExamine(@PathVariable Long id){
        return textualMaterialService.updateTextExamine(id);
    }
    @RequestMapping("updateTextIsDelete2Zero")
    public JsonResult updateTextIsDelete2Zero(@PathVariable Long id){
        return textualMaterialService.updateTextIsDelete2Zero(id);
    }
    /**给微信小程序设置的API*/
    @RequestMapping("selectAllTextsToWX")
    public JsonResult selectAllTextsToWX(String search){
        return textualMaterialService.selectAllTextsToWX(search);
    }

    @RequestMapping("textOrdinaryCount")
    public JsonResult textOrdinaryCount(){
        return textualMaterialService.textOrdinaryCount();
    }

    @RequestMapping("textVipCount")
    public JsonResult textVipCount(){
        return textualMaterialService.textVipCount();
    }

}
